package br.com.afi.util.log.writer;

import java.text.MessageFormat;

import org.apache.log4j.Level;
import org.apache.log4j.Priority;

import br.com.afi.util.log.Logger;

/**
 * Essa classe escreve utilizando o Log4j.
 *
 * @author André de Fontana Ignacio
 * @version 1.0
 */
public class Log4jWriter implements LoggerWriter {
	private org.apache.log4j.Logger log;
	private boolean infoEnabled;
	private boolean debugEnabled;
	private boolean warnEnabled;
	private boolean errorEnabled;
	private boolean fatalEnabled;
	private boolean traceEnabled;
	
	/**
	 * Construtor
	 */
	public Log4jWriter()
	{
		super();
	}
	
	/**
	 * Cria um Logger do Log4J
	 * 
	 * @param clazz Classe
	 */
	public Log4jWriter( Class<?> clazz )
	{
		super();
		configure(clazz);
	}
	
	/**
	 * Cria um Logger do Log4j
	 * 
	 * @param name Nome do log
	 */
	public Log4jWriter( String name )
	{
		super();
		configure(name);
	}
	
	public void configure( Class<?> clazz ) 
	{
		log = org.apache.log4j.Logger.getLogger(clazz);
		infoEnabled = log.isEnabledFor(Level.INFO);
		debugEnabled = log.isEnabledFor(Level.DEBUG);
		traceEnabled = log.isEnabledFor(Level.TRACE);
		warnEnabled = log.isEnabledFor(Level.WARN);
		errorEnabled = log.isEnabledFor(Level.ERROR);
		fatalEnabled = log.isEnabledFor(Level.FATAL);
	}

	public void configure( String name ) 
	{
		log = org.apache.log4j.Logger.getLogger(name);
		infoEnabled = log.isEnabledFor(Level.INFO);
		debugEnabled = log.isEnabledFor(Level.DEBUG);
		traceEnabled = log.isEnabledFor(Level.TRACE);
		warnEnabled = log.isEnabledFor(Level.WARN);
		errorEnabled = log.isEnabledFor(Level.ERROR);
		fatalEnabled = log.isEnabledFor(Level.FATAL);
	}

	/**
	 * Cria um Logger.
	 * 
	 * @param clazz Classe
	 */
	public static Logger getLogger( Class<?> clazz )
	{
		return Logger.getLogger( clazz );
	}
	
	/**
	 * Cria um Logger.
	 * 
	 * @param name Nome do log
	 */
	public static Logger getLogger( String name )
	{
		return Logger.getLogger( name );
	}
	
	private void log( Priority priority, Object message, Object...args)
	{
		log.log( priority, MessageFormat.format( message.toString(),args ) );
	}
	
	private void log( Priority priority, Object message, Throwable t, Object...args)
	{
		log.log( priority, MessageFormat.format( message.toString(),args ), t );
	}

	public void debug( Object message, Throwable t, Object... args) 
	{
		if( debugEnabled )
		{
			log(Level.DEBUG, message, t, args);
		}
	}
	
	public void debug( Object message, Object... args )
	{
		if( debugEnabled )
		{
			log(Level.DEBUG, message, args);
		}
	}
	
	public void trace( Object message, Throwable t, Object... args) 
	{
		if( traceEnabled )
		{
			log(Level.TRACE, message, t, args);
		}
	}
	
	public void trace( Object message, Object... args )
	{
		if( traceEnabled )
		{
			log(Level.TRACE, message, args);
		}
	}

	public void error( Object message, Throwable t, Object... args ) 
	{
		if( errorEnabled )
		{
			log(Level.ERROR, message, t, args);
		}
	}

	public void error(Object message, Object... args) 
	{
		if( errorEnabled )
		{
			log(Level.ERROR, message, args);
		}
	}
	
	public void fatal(Object message, Throwable t, Object... args) 
	{
		if( fatalEnabled )
		{
			log(Level.FATAL, message, t, args);
		}
	}

	public void fatal(Object message, Object... args) 
	{
		if( fatalEnabled )
		{
			log( Level.FATAL, message, args );
		}
	}
	
	public void info(Object message, Throwable t, Object... args) 
	{
		if( infoEnabled )
		{
			log( Level.INFO, message, t, args );
		}
	}

	public void info(Object message, Object... args) 
	{
		if( infoEnabled )
		{
			log( Level.INFO, message, args );
		}
	}

	public void warn(Object message, Throwable t, Object... args) 
	{
		if( warnEnabled )
		{
			log( Level.WARN, message, t, args );
		}
	}

	public void warn(Object message, Object... args) 
	{
		if( warnEnabled )
		{
			log( Level.WARN, message, args );
		}
	}
}
